package com.zbs.simbot.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 读取与写入JSON文件
 * */

@Slf4j
public class JsonFileUtil {

    public JsonFileUtil() {
    }

    // 将Nodes信息保存到本地json文件中
    public static String saveJson(Object obj, String filePath){
        String writeString = JSON.toJSONString(obj, JSONWriter.Feature.PrettyFormat);
        BufferedWriter writer = null;
        File file = FileUtil.createNewFile(filePath);
        if (file == null) {
            return "";
        }
        //如果多次执行同一个流程，会导致json文件内容不断追加，在写入之前清空文件
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), StandardCharsets.UTF_8));
            writer.write("");
            writer.write(writeString);
        }catch (IOException e){
            log.error("写入JSON出错：{}",e.getMessage());
            log.error("写入JSON出错:",e);
            return "";
        }finally {
            try{
                if (writer != null){
                    writer.close();
                }
            }catch (IOException e){
                log.error("关闭IO出错：{}",e.getMessage());
                log.error("关闭IO出错:",e);
            }
        }
        return filePath;
    }

    // 用于读取JSON文件
    public static String readJsonFile(String filePath){
        File file = new File(filePath);
        if (!file.exists()) {
            return "";
        }
        BufferedReader reader = null;
        StringBuilder readJson = new StringBuilder();
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            reader = new BufferedReader(inputStreamReader);
            String tempString;
            while ((tempString = reader.readLine()) != null){
                readJson.append(tempString);
            }
        }catch (IOException e){
            log.error("读取JSON出错：{}",e.getMessage());
            log.error("读取JSON出错:",e);
        }finally {
            if (reader != null){
                try {
                    reader.close();
                }catch (IOException e){
                    log.error("关闭IO出错：{}",e.getMessage());
                    log.error("关闭IO出错:",e);
                }
            }
        }
        return readJson.toString();
    }

    public static <T> T readJsonObjectFile(String filePath,Class<T> clazz) {
        String json = readJsonFile(filePath);
        try {
            return JSONObject.parseObject(json,clazz);
        } catch (Exception e) {
            log.error("读取文件转JSON出错：{}",e.getMessage());
            log.error("读取文件转JSON出错:",e);
            return null;
        }
    }

    public static <T> List<T> readJsonArrayFile(String filePath, Class<T> clazz) {
        String json = readJsonFile(filePath);
        try {
            return JSON.parseArray(json,clazz);
        } catch (Exception e) {
            log.error("读取文件转JSON出错：{}",e.getMessage());
            log.error("读取文件转JSON出错:",e);
            return null;
        }
    }


    public static <T> T readJsonObjectFile(String filePath, TypeReference<T> typeReference) {
        String json = readJsonFile(filePath);
        try {
            return JSON.parseObject(json,typeReference);
        } catch (Exception e) {
            log.error("读取文件转JSON出错：{}",e.getMessage());
            log.error("读取文件转JSON出错:",e);
            return null;
        }
    }


}